home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / popularity-contest / popcon-upload-ubuntu < prev    next >
Encoding:
Text File  |  2007-02-05  |  1.5 KB  |  49 lines

  1. #!/usr/bin/env python
  2.  
  3. import httplib, mimetypes, sys
  4.  
  5. def post_multipart(host, uri, fields, files):
  6.     content_type, body = encode_multipart_formdata(fields,files)
  7.     h = httplib.HTTPConnection(host)
  8.     h.putrequest('POST', uri)
  9.     h.putheader('content-type', content_type)
  10.     h.putheader('content-length', str(len(body)))
  11.     h.endheaders()
  12.     h.send(body)
  13.     response = h.getresponse()
  14.     return response.read()
  15.  
  16. def encode_multipart_formdata(fields, files):
  17.     BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
  18.     CRLF = '\r\n'
  19.     L = []
  20.     for (key, value) in fields:
  21.         L.append('--' + BOUNDARY)
  22.         L.append('Content-Disposition: form-data; name="%s"' % key)
  23.         L.append('')
  24.         L.append(value)
  25.     for (key, filename, value) in files:
  26.         L.append('--' + BOUNDARY)
  27.         L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
  28.         L.append('Content-Type: %s' % get_content_type(filename))
  29.         L.append('')
  30.         L.append(value)
  31.     L.append('--' + BOUNDARY + '--')
  32.     L.append('')
  33.     body = CRLF.join(L)
  34.     content_type= 'multipart/form-data; boundary=%s' % BOUNDARY
  35.     return content_type, body
  36.  
  37. def get_content_type(filename):
  38.     return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
  39.  
  40. upfile = 'popcon-data'
  41. uploadname = 'popcondata'
  42. val = sys.stdin.read()
  43.  
  44. data = ((uploadname,upfile,val),)
  45. ret= post_multipart('popcon.ubuntu.com', '/popcon-submit.cgi', '' , data)
  46. if not ret == 'Thanks!\n': 
  47.     sys.stderr.write("%s\n" % ret)
  48.     sys.exit(1)
  49.